package com.cblz.bd.server.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cblz.bd.common.core.entity.BaConfig;
import com.cblz.bd.common.core.utils.ResultResponse;
import com.cblz.bd.common.core.utils.SearchParamsUtil;
import com.cblz.bd.server.core.annotation.AdminApiController;
import com.cblz.bd.server.core.service.BaConfigService;
import io.vavr.Tuple3;
import io.vavr.Tuple5;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * {@code @Name:} BaConfigController
 * {@code @Description: } 系统配置类控制层
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2023/2/14 - 周二
 */
@RequiredArgsConstructor
@AdminApiController("routine.config")
public class BaConfigController {

    private final BaConfigService baConfigService;

    /**
     * 分页查询
     *
     * @param searchParams 搜索条件
     * @return 分页结果数据
     */
    @PostMapping("index")
    public ResultResponse index(@RequestBody(required = false) Map<String, Object> searchParams) {
        Tuple5 tuple5 = SearchParamsUtil.searchParamsHandle2(searchParams);
        Tuple3<Page<BaConfig>, QueryWrapper<BaConfig>, Object> queryWrapperObjectTuple3 = SearchParamsUtil.params2QueryWrapper(new BaConfig(), tuple5);

        return new ResultResponse().data(baConfigService.getAllInfoByPage(queryWrapperObjectTuple3));
    }

    @GetMapping("index")
    public ResultResponse indexByAdmin() {
        return new ResultResponse().data(baConfigService.getAllInfoByAdmin());
    }

    /**
     * 新增
     *
     * @param baConfig 实体对象
     */
    @GetMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public void add(@RequestBody BaConfig baConfig) {
        baConfigService.save(baConfig);
    }

    /**
     * 修改前查询
     *
     * @param id id
     * @return
     */
    @GetMapping("edit")
    public ResultResponse editBefore(@RequestParam Integer id) {
        return new ResultResponse().data(baConfigService.getById(id));
    }

    /**
     * 修改
     *
     * @param data 实体对象
     */
    @PostMapping("edit")
    @Transactional(rollbackFor = Exception.class)
    public ResultResponse edit(@RequestBody Map data) {
        baConfigService.editConfigInfo(data);
        return new ResultResponse().message("操作成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("del")
    @Transactional(rollbackFor = Exception.class)
    public void del(@RequestBody Map map) {
        baConfigService.removeByIds((List) map.get("ids"));
    }
}
